<template lang="pug">
div
  Card(v-if='question')
    h3 {{ question.title }}
    div 提问者： {{ question.owner.name }}
    div 提问时间： {{ createTime }}

  Form(:model='formData', :label-width=80)
    FormItem(label='回复人')
      div {{ answer.owner.name }}
    FormItem(label='回复内容')
      Input(type='textarea', v-model='formData.body')
      p 目前只支持文字回复
    FormItem
      Button(type='primary', :loading='loading', @click='submit') 提交
</template>

<script>
import questionApi from '@/apis/question'

export default {
  data () {
    return {
      loading: false,
      question: null,
      answer: null,
      formData: {
        qid: this.$route.params.qid,
        aid: this.$route.params.aid,
        // title: `问题${this.$route.params.id}的答案`,
        body: ''
      }
    }
  },
  computed: {
    createTime () {
      let date = new Date(this.question.created * 1000)
      return [date.getFullYear(), 1 + date.getMonth(), date.getUTCDate()].join('-')
      // return date
    }
  },
  mounted () {
    questionApi.getQuestion(this.$route.params.qid)
      .then(data => {
        this.question = data.data
      })
    questionApi.getQuestionAnswer(this.$route.params.aid)
      .then(data => {
        this.answer = data.data
        this.formData.title = data.data.title
        this.formData.body = data.data.body
      })
  },
  methods: {
    submit () {
      this.loading = true
      questionApi.patchQuestionAnswer(this.formData).then(() => {
        this.loading = false
        this.$Message.success('提交成功')
        this.$router.replace(`/questions/${this.$route.params.qid}`)
      })
    }
  }
}
</script>

